<!DOCTYPE html>
<html lang="en">
<title>编辑教练课时费: ${(coach.nickName)!}教练</title>
<#import "/layout/layout.html" as body/>
<@body.page>
<link href="${baseUrl}css//plugins/magicsuggest/magicsuggest-min.css" rel="stylesheet">

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title overflow">
                    <h5 class="pull-left">编辑教练课时费: ${(coach.nickName)!}教练</h5>
                </div>
                <div class="ibox-content">
                    <form id="addForm" class="form-horizontal">
                        <input type="hidden" id="id" name="id" value="${(coachFee.id)!}">
                        <input type="hidden" id="coachId" name="coachId" value="${coach.id}">

                        <div class="form-group"><h5 class="col-sm-12 margin-top">团课结算</h5></div>
                        <div class="hr-line-dashed"></div>
                        <!--<div class="" style="display: none;">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">基础课时费1</label>
                                <div class="col-sm-10">
                                    <input type="text" id="teamBasicFee" name="teamBasicFee" class="form-control" placeholder="请填写该类型对应的基础课时费">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">对应结算课程</label>
                                <input id="standCourse" name="standCourse" type="hidden">
                                <div class="col-sm-10">
                                    <div id="courseStandards"></div>
                                </div>
                            </div>
                        </div>-->

                        <!--基础课时费---对应结算课程--box-->
                        <div id="for_basics"></div>

                        <!--添加课时费类型-->
                        <div id="add_class_fee"></div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">爆场奖励费</label>
                            <div class="col-sm-10">
                                <input type="text" id="teamCourseFullFee" name="teamCourseFullFee" value="${(coachFee.teamCourseFullFee)!}" class="form-control" placeholder="请填写该教练的爆场奖励费">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">白板课补贴费</label>
                            <div class="col-sm-10">
                                <input type="text" id="teamCourseBlankFee" name="teamCourseBlankFee" value="${(coachFee.teamCourseBlankFee)!}" class="form-control" placeholder="请填写该教练的白板课补贴费">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <!--私教结算-->
                        <div class="form-group"><h5 class="col-sm-12 margin-top">私教结算</h5></div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">分成比例</label>
                            <div class="col-sm-10">
                                <input type="text" id="privateCourseShareRate" name="privateCourseShareRate" value="${(coachFee.privateCourseShareRate)!}" class="form-control" placeholder="请填写该教练私教课的分成比例">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <!--檬营结算-->
                        <div class="form-group"><h5 class="col-sm-12 margin-top">檬营结算</h5></div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">基础课时费</label>
                            <div class="col-sm-10">
                                <input type="text" id="campCourseBasicFee" name="campCourseBasicFee" value="${(coachFee.campCourseBasicFee)!}" class="form-control" placeholder="请填写檬营基础课时费">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">檬营结算分隔金额</label>
                            <div class="col-sm-10">
                                <input type="text" readonly value="<#if coachFee?? && coachFee.campCourseSplitMoney??>${coachFee.campCourseSplitMoney}<#else>10000</#if>" id="campCourseSplitMoney" name="campCourseSplitMoney" class="form-control" placeholder="请填写檬营结算分隔金额">
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">＞<span id="split"></span>元的分成比例</label>
                            <div class="col-sm-10">
                                <input type="text" id="campCourseMoreShareRate" name="campCourseMoreShareRate" value="${(coachFee.campCourseMoreShareRate)!}" class="form-control" placeholder="请填写檬营营收金额大于檬营结算分隔金额时的分成比例">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">≤<span id="split2"></span>元的分成比例</label>
                            <div class="col-sm-10">
                                <input type="text" id="campCourseAndLessShareRate" name="campCourseAndLessShareRate" value="${(coachFee.campCourseAndLessShareRate)!}" class="form-control" placeholder="请填写檬营营收金额小于等于檬营结算分隔金额时的分成比例">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">备注</label>
                            <div class="col-sm-10">
                                <input type="text" id="remark" name="remark" value="${(coachFee.remark)!}" class="form-control" placeholder="内部备注字段">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="ibox-content overflow">
                    <div class="col-sm-12">
                        <button type="button" onclick="window.history.go(-1);" class="btn btn-default pull-left">取消编辑</button>
                        <!--<button type="button" id="xx_xx" class="btn btn-default pull-left">取消编辑</button>-->
                        <button type="button" id="submit" class="btn btn-primary pull-right">确认编辑</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    //var courseStandards = ${courseStandards!};//标准课程 ID list
    var no_basics = [{"teamCourseBasicFee": '', "teamCourseStandCourseId": ''}];//团课基础课时费 list
    var $basics = ${basics!};
    var basics = $basics.length > 0 ? $basics : no_basics;//团课基础课时费 list
    var standIds = [];
    var check = true;
    var baseData = ${courseStandards!};
    var magicSuggest_data = {
        data: baseData,
        placeholder: '请选择',
        maxSelection: 50,
        required: true,
        expandOnFocus: true,
        strictSuggest: true,
        displayField: 'name',
        valueField: 'id'
    };

    $(document).ready(function () {
//        debugger;

        basics.forEach(function (value, index) {
            var _html;
            _html = index === 0 ? '<div class="fee_box"><div class="form-group"><label class="col-sm-2 control-label">基础课时费1</label><div class="col-sm-10"><input type="text" name="teamBasicFee" class="form-control" value="' + value.teamCourseBasicFee + '" placeholder="请填写该类型对应的基础课时费"></div></div>'
                + '<div class="form-group"><label class="col-sm-2 control-label">对应结算课程</label><input name="standCourse" type="hidden"><input name="standCourse1" type="hidden" value="' + value.teamCourseStandCourseId + '"><div class="col-sm-10"><div id="magicSuggest_' + index + '"></div></div></div><div class="overflow text-right" style="margin-bottom: 15px"><a href="javascript:" id="click_add_class_fee">添加课时费类型</a></div></div>' : '<div class="fee_box"><div class="form-group"><label class="col-sm-2 control-label">基础课时费' + Number(index + 1)
                + '</label><div class="col-sm-10"><input type="text" name="teamBasicFee" class="form-control" value="' + value.teamCourseBasicFee + '" placeholder="请填写该类型对应的基础课时费"></div></div>'
                + '<div class="form-group"><label class="col-sm-2 control-label">对应结算课程</label><input name="standCourse" type="hidden"><input name="standCourse1" type="hidden" value="' + value.teamCourseStandCourseId + '"><div class="col-sm-10"><div id="magicSuggest_' + index + '"></div></div></div><div class="overflow text-right" style="margin-bottom: 15px"><a href="javascript:" id="remover_class_fee_' + index + '">删除</a></div></div>';
            $("#for_basics").append(_html);

            var $magicSuggest_ = '#magicSuggest_' + index, $remover__fee = '#remover_class_fee_' + index;
            var cd = value.teamCourseStandCourseId.split(",");
            var sm = 'sm_' + index;
            sm = $($magicSuggest_).magicSuggest(magicSuggest_data);
            if ($basics.length > 0) {
                sm.setValue(cd);
            }
            var gv1 = $($magicSuggest_).parents('.fee_box').find('input[name="teamBasicFee"]').val() + "=" + cd;
            var rgv1 = gv1.replace(/,/g, '&');
            $($magicSuggest_).parents('.fee_box').find('input[name="standCourse"]').val(rgv1);

            $(sm).on('blur', function () {
                var gv = $($magicSuggest_).parents('.fee_box').find('input[name="teamBasicFee"]').val() + "=" + this.getValue();
                var rgv = gv.replace(/,/g, '&');
                console.log(rgv);
                console.log(this.getValue());
                $($magicSuggest_).parents('.fee_box').find('input[name="standCourse"]').val(rgv);
                $($magicSuggest_).parents('.fee_box').find('input[name="standCourse1"]').val(this.getValue());
            });
            //只改 基础课时费 时
            $($magicSuggest_).parents('.fee_box').find('input[name="teamBasicFee"]').on('blur', function () {
                var gvv = $(this).val() + "=" + $($magicSuggest_).parents('.fee_box').find('input[name="standCourse1"]').val();
                var rgvv = gvv.replace(/,/g, '&');
                console.log(rgvv);
                $($magicSuggest_).parents('.fee_box').find('input[name="standCourse"]').val(rgvv)
            });

            $($remover__fee).on('click', function () {
                $(this).parents('.fee_box').remove();
                standIds = standIds.filter(function ($item) {
                    return $item.key !== $magicSuggest_
                });
            });

        });

        var counter = basics.length;
        $("#click_add_class_fee").click(function () {
            counter += 1;
            var html = '<div class="fee_box"><div class="form-group"><label class="col-sm-2 control-label">基础课时费' + counter
                + '</label><div class="col-sm-10"><input type="text" name="teamBasicFee" class="form-control" placeholder="请填写该类型对应的基础课时费"></div></div>'
                + '<div class="form-group"><label class="col-sm-2 control-label">对应结算课程</label><input name="standCourse" type="hidden"><input name="standCourse1" type="hidden"><div class="col-sm-10"><div id="magicSuggest_' + counter + '"></div></div></div><div class="overflow text-right" style="margin-bottom: 15px"><a href="javascript:" id="remover_class_fee_' + counter + '">删除</a></div></div>';
            $("#add_class_fee").append(html);

            var $magicSuggest = '#magicSuggest_' + counter, $remover_class_fee = '#remover_class_fee_' + counter;

            $($($magicSuggest).magicSuggest(magicSuggest_data)).on('blur', function () {
                var _gv = $($magicSuggest).parents('.fee_box').find('input[name="teamBasicFee"]').val() + "=" + this.getValue();
                var _rgv = _gv.replace(/,/g, '&');
                console.log(_rgv);
                console.log(this.getValue());
                $($magicSuggest).parents('.fee_box').find('input[name="standCourse"]').val(_rgv);
                $($magicSuggest).parents('.fee_box').find('input[name="standCourse1"]').val(this.getValue());
            });
            //只改 基础课时费 时
            $($magicSuggest).parents('.fee_box').find('input[name="teamBasicFee"]').on('blur', function () {
                var gvv = $(this).val() + "=" + $($magicSuggest).parents('.fee_box').find('input[name="standCourse1"]').val();
                var rgvv = gvv.replace(/,/g, '&');
                console.log(rgvv);
                $($magicSuggest).parents('.fee_box').find('input[name="standCourse"]').val(rgvv)
            });
            $($remover_class_fee).click(function () {
                $(this).parents('.fee_box').remove();
                standIds = standIds.filter(function ($item) {
                    return $item.key !== $magicSuggest
                });
            })

        });

        // 檬营结算分隔金额
        var $campCourseSplitMoney = $("#campCourseSplitMoney");
        $campCourseSplitMoney.keyup(function () {
            $("#split,#split2").text($(this).val());
        });
        $campCourseSplitMoney.trigger("keyup");

        $("#submit").click(function () {
            if ($('input[name="standCourse"]').val().length === 0) {
                swal("错误提示", "请先填写完整基础课时费!", "error");
                return false;
            }

            var array_course = [];
            var error_a = [];
            $('[name="standCourse1"]').each(function () {
                var tv = $(this).val();
                var tvs = tv.split(',');
                console.log(tv);
                for (var j = 0; j < tvs.length; j++) {
                    if (array_course.indexOf(tvs[j]) !== -1) {
                        error_a.push(tvs[j]);
                    }
                }
                array_course = array_course.concat(tvs);
            });

            console.log('已选对应结算课程ID：' + array_course, '重复：' + error_a);
            if (error_a.length > 0) {
                swal("错误提示", "选择对应结算课程不能重复,请核对！", "error");
                return false;
            }

            if ($("#teamCourseFullFee").val().length === 0) {
                swal("错误提示", "请先填写爆场奖励费!", "error");
                return false;
            }
            if ($("#teamCourseBlankFee").val().length === 0) {
                swal("错误提示", "请先填写白板课补贴费!", "error");
                return false;
            }
            if ($("#privateCourseShareRate").val().length === 0) {
                swal("错误提示", "请先填写私教分成比例!", "error");
                return false;
            }
            if ($("#campCourseBasicFee").val().length === 0) {
                swal("错误提示", "请先填写檬营基础课时费!", "error");
                return false;
            }
            if ($campCourseSplitMoney.val().length === 0) {
                swal("错误提示", "请先填写檬营结算分隔金额!", "error");
                return false;
            }
            if ($("#campCourseMoreShareRate").val().length === 0) {
                swal("错误提示", "请先填写檬营营收金额大于檬营结算分隔金额时的比例!", "error");
                return false;
            }
            if ($("#campCourseAndLessShareRate").val().length === 0) {
                swal("错误提示", "请先填写檬营营收金额小于等于檬营结算分隔金额时的比例!", "error");
                return false;
            }
            submit();
        });
        /**
         * 保存
         */
        function submit() {
            var param = $("#addForm").serialize();
            debugger;
            $.post(
                "save",
                param,
                function (result) {
                    //debugger;
                    if (result === "true") {
                        swal("保存成功!", "页面即将重新加载,请稍后...", "success");
                        window.setTimeout(function () {
                            //window.location = "coachCourseFeeEdit.html?coachId=${coach.id}";
                            window.location = "coachCourseFeeList.html?coachId=${coach.id}";
                        }, 1000);
                    } else {
                        swal("保存失败!", '', "error");
                    }
                }
            );
        }

    });
</script>
</@body.page>
</html>